﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace TZReport_Util.Util
{
    public class CacherUtils
    {
        private static readonly Cache cache = null;

        static CacherUtils()
        {
            cache = HttpContext.Current.Cache;
        }

        /// <summary>
        /// 相对过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="minute"></param>
        public static void InsNoAbsoluteExpirationCacheWithMinute(string key, object value, int minute = 5)
        {
            cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(minute), CacheItemPriority.High, null);
        }

        /// <summary>
        /// 绝对过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="minute"></param>
        public static void InsNoSlidingExpirationCacheWithMinute(string key, object value, int minute = 5)
        {
            cache.Insert(key, value, null, DateTime.Now.AddMinutes(minute), Cache.NoSlidingExpiration);
        }

        public static void InsNoSlidingExpirationCacheDay(string key, object value, int days = 5)
        {
            cache.Insert(key, value, null, DateTime.Now.AddDays(days), Cache.NoSlidingExpiration);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            return cache.Get(key);
        }

        public static void RemoveCache(string key)
        {
            try
            {
                cache.Remove(key);
            }
            catch { };
        }
    }
}
